<?php

class Node {

    private $ID = null;
    private $obj = null;
    private $parentNode = null;
    private $treeArray = null;

    public function __input( $obj ) {
        $this->obj = $obj;
        $this->treeArray = null;
        $this->parentNode = null;
        $this->ID = null;
        return $this;
    }

    public function dataTree( $items, $id = 'id', $pid = 'sid' ) {
        $tree = array(); //格式化好的树
        foreach ( $items as $item ) {
            if ( isset( $items[ $item[ $pid ] ] ) ) {
                $items[ $item[ $pid ] ][ 'child' ][ ] = & $items[ $item[ $id ] ];
            } else {
                $tree[ $item[ $id ] ] = & $items[ $item[ $id ] ];
            }
        }
        return $tree;
    }

    # 多级分类 数组列表
    public function dataTreeArray( $result, $id = 0, $sid = "sid", $name = 'title', $icon = "├", $node = 0 ) {
        if ( !$result ) return false;
        foreach ( $result as $v ) {
            if ( intval( $v[ $sid ] ) === intval( $id ) ) {
                $l = str_repeat( "&nbsp;&nbsp;", $node ) . ( $node ? $icon : "" );
                $v[ 'node' ] = $node;
                $v[ 'treeTitle' ] = $l . $v[ $name ];
                $this->treeArray[ $v[ 'id' ] ] = $v;
                $this->dataTreeArray( $result, $v[ 'id' ], $sid, $name, $icon, $node + 1 );
            }
        }
        return $this->treeArray;
    }

    public function dataTreeLiUl( $items, $style = 'submenu submenu-', $child = 0 ) {
        $html = $child ? "\n<ul{$style}>\n" : '';
        $current = '';
        foreach ( $items as $item ) {
            $method_url = ( $item[ 'model' ] == 'page' ? 'archives' : 'sort' ) . "_url";
            $url = $this->obj->$method_url( $item );
            $split = $child ? "\t" : '';
            if ( R( 'id' ) == $item[ 'id' ] ) {
                $current = ' current';
            }
            if ( $alias = R( 'alias' ) ) {
                if ( $alias == $item[ 'alias' ] ) {
                    $current = ' current';
                }
            }
            $html .= "{$split}<li class=\"item item-{$item[ 'id' ]}{$current}\"><a href=\"{$url}\" title=\"{$item[ 'title' ]}\"><span>{$item[ 'title' ]}</span></a>";
            if ( $item[ 'child' ] ) {
                $html .= $this->dataTreeLiUl( $item[ 'child' ], " class=\"{$style}{$item[ 'id' ]}\"", $child + 1 );
            }
            $html .= "</li>\n";
        }
        $html .= $child ? "</ul>\n" : '';
        return $html;
    }

    # 多级分类 li列表
    public function dataTreeLI( $result ) {
        $html = $current = "";
        foreach ( $result as $v ) {
            $method_url = ( $v[ 'model' ] == 'page' ? 'archives' : 'sort' ) . "_url";
            $url = $this->obj->$method_url( $v );
            if ( R( 'id' ) == $v[ 'id' ] ) {
                $current = ' current';
            }
            if ( $alias = R( 'alias' ) ) {
                if ( $alias == $v[ 'alias' ] ) {
                    $current = ' current';
                }
            }
            $html .= "<li class=\"node{$current}\"><a href=\"{$url}\" title=\"{$v[ 'title' ]}\">{$v[ 'title' ]}</a></li>\n";
        }
        return $html;
    }

    # 分类下所有子分类ID
    public function treeID( $arr, $id, $node = 0 ) {
        if ( $node <= 0 ) {
            $this->ID[ $id ] = $id;
        }
        foreach ( $arr as $v ) {
            if ( intval( $v[ 'sid' ] ) === intval( $id ) ) {
                $this->ID[ $v[ 'id' ] ] = $v[ 'id' ];
                $this->treeID( $arr, $v[ 'id' ], $node++ );
            }
        }
        return $this->ID;
    }

    # 分类下所有子分类ID
    public function treeAlias( $arr, $alias = null ) {
        $root = 0;
        foreach ( $arr as $v ) {
            if ( $v[ 'alias' ] == $alias ) {
                $root = $v[ 'id' ];
                break;
            }
        }
        if ( $root ) {
            return $this->treeID( $arr, $root );
        }
    }


    public function parentTree( $arr, $sid, $id, $type = 'archives' ) {
        if ( intval( $sid ) <= 0 ) {
            $method_url = $type . "_url";
            $arr = $arr[ $id ];
            $url = $this->obj->$method_url( $arr );
            if ( $id != -1 ) {
                $this->parentNode .= " > <a href=\"{$url}\" title=\"{$arr[ 'title' ]}\">{$arr[ 'title' ]}</a>";
            }
        } else {
            foreach ( $arr as $v ) {
                if ( intval( $v[ 'id' ] ) === intval( $sid ) ) {
                    $method_url = $type . "_url";
                    $url = $this->obj->$method_url( $v );
                    $this->parentNode = " > <a href=\"{$url}\" title=\"{$v[ 'title' ]}\">{$v[ 'title' ]}</a>" . $this->parentNode;
                    $this->parentTree( $arr, $v[ 'sid' ], $id, $type );
                }
            }
        }
        return $this->parentNode;
    }
}